
var AppTaskMe = angular.module('taskme',['$strap.directives','textAngular']);
AppTaskMe.factory('UserService', function() {
	return {
		authorTaskMe : (currentUser.length != 0) ? true : false
	};
});
AppTaskMe.controller('MainController', function($scope,$http,$modal,UserService) {
	var heightMainPage = $(window).height()-75;
	$scope.heightMainPage = heightMainPage+"px";
	
	$scope.isGuest = UserService.authorTaskMe;
	
	$scope.linkSlogan = "#";
	$scope.titleSlogan = "Never be afraid to say what you feel.";
	$scope.aboutSlogan = "Amir Hadjihabib,";
	$.get("/api/reviews/getSlogan",{})
	.success(function(data, status, headers, config) {
		$scope.$apply(function() {
			if(!data.error){
				var d = data.data;
				$scope.linkSlogan = "#";
				$scope.titleSlogan = d.content;
				if(d.about == null)
					$scope.aboutSlogan = "";
				else
					$scope.aboutSlogan = d.about+", ";
			}
		});
	}).error(function(data, status, headers, config) {
		console.log(data);
		console.log(status);
	});
	
	
});
AppTaskMe.controller('HeaderController', function($scope,$http,$modal,UserService) {
	$scope.authorTaskMe = UserService.authorTaskMe;
	$scope.avatarHeader = false;
	$scope.linkAvatar = "/upload/user/default/no-photo.png";
	if($scope.authorTaskMe && currentUser.avatar.status){
		$scope.avatarHeader = true;
		$scope.linkAvatar = currentUser.avatar.link;
	}else{
		
		$scope.avatarHeader = false;
		
	}
	$scope.fullname = "";
	$scope.linkMenu = {
		createProject:"/",
		profile:"/",
		logout:"/"
	};
	if($scope.authorTaskMe){
		$scope.fullname = currentUser.user.first_name+" "+currentUser.user.last_name;
		$scope.linkMenu = {
			createProject:"/dashboard",
			profile:"/profile?id="+currentUser.user.id,
			logout:"/user/logout"
		};
	}
	
	
});
AppTaskMe.controller('LoginController', function($scope,$http,UserService) {
	$scope.hideMessage = true;
	$scope.email = "";
	$scope.password = "";
	$scope.remember = false;
	
	$scope.login = function(){
		$.post("/api/users/login", {"email":$scope.email,"password":$scope.password,"remember":$scope.remember})
		.success(function(data, status, headers, config) {
			$scope.$apply(function() {
				$scope.emailPassWordHide = true;
				$scope.emailPassWord = "";
				$scope.emailMessageHide = true;
				$scope.emailMessage = "";
				if(data.error){
					if(data.model.length != 0){
						$.each(data.model,function(x,y){
							switch(x){
								case "email":
									$scope.emailMessageHide = false;
									$scope.emailMessage = (typeof y[0]!="undefined") ? y[0] : "";
								break;
								case "password":
									$scope.emailPassWordHide = false;
									$scope.emailPassWord = (typeof y[0]!="undefined") ? y[0] : "";
								break;
							}
						});
					}else{
					
					}
				}else{

					$(".close").trigger('click');
					window.location.href = "/";
				}
			});
		}).error(function(data, status, headers, config) {
			console.log(data);
			console.log(status);
		});
	}
});
AppTaskMe.controller('SignUpController', function($scope,$http) {
	$scope.firstname = "";
	$scope.lastname = "";
	$scope.username = "";
	$scope.email = "";
	$scope.password = "";
	$scope.confirm_password = "";
	
	$scope.firstMessageHide = true;
	$scope.lastMessageHide = true;
	$scope.usernameMessageHide = true;
	$scope.emailMessageHide = true;
	$scope.passwordMessageHide = true;
	$scope.confirm_passwordMessageHide = true;
	
	$scope.register = function(){
		$.post("/api/users/register", {
			"first_name":$scope.firstname,
			"last_name":$scope.lastname,
			"username":$scope.username,
			"email":$scope.email,
			"password":$scope.password,
			"confirm_password":$scope.confirm_password
		}).success(function(data, status, headers, config) {
			$scope.$apply(function() {
				$scope.usernameMessageHide = true;
				$scope.usernameMessage = "";
				$scope.emailMessageHide = true;
				$scope.emailMessage = "";
				
				$scope.firstMessageHide = true;
				$scope.firstMessage = "";
				
				$scope.lastMessageHide = true;
				$scope.lastMessage = "";
				
				$scope.passwordMessageHide = true;
				$scope.passwordMessage = "";				
				
				$scope.confirm_passwordMessageHide = true;
				$scope.confirm_passwordMessage = "";
				if(data.error){
					if(data.model.length != 0){
						$.each(data.model,function(x,y){
							switch(x){
								case "email":
									$scope.emailMessageHide = false;
									$scope.emailMessage = (typeof y[0]!="undefined") ? y[0] : "";
								break;
								case "username":
									$scope.usernameMessageHide = false;
									$scope.usernameMessage = (typeof y[0]!="undefined") ? y[0] : "";
								break;
								case "first_name":
									$scope.firstMessageHide = false;
									$scope.firstMessage = (typeof y[0]!="undefined") ? y[0] : "";
								break;
								case "last_name":
									$scope.lastMessageHide = false;
									$scope.lastMessage = (typeof y[0]!="undefined") ? y[0] : "";
								break;
								case "password":
									$scope.passwordMessageHide = false;
									$scope.passwordMessage = (typeof y[0]!="undefined") ? y[0] : "";
								break;								
								case "confirm_password":
									$scope.confirm_passwordMessageHide = false;
									$scope.confirm_passwordMessage = (typeof y[0]!="undefined") ? y[0] : "";
								break;
							}
						});
					}else{
					
					}
				}else{
					$.post("/api/mail/register", {
					
					}).success(function(data, status, headers, config) {
					
					}).error(function(data, status, headers, config) {
						
					});
					$(".close").trigger('click');
					window.location.href = "/";
				}
			});
		}).error(function(data, status, headers, config) {
			console.log(data);
			console.log(status);
		});
	}
});
AppTaskMe.controller('NewsController', function($scope,$http) {

});

$(document).ready(function(){
	$('#rotatescroll').tinycircleslider({ interval: false, snaptodots: true });
});
